*Program to analyze quarterly data to generate aggregate growth distribution;
*Created Feb 2, 2015;
*Revisions 2023 02 28;
*Revisions 2024 04 16;

libname input "/OEUS/users/FEDSusers/QCEW/data/QuartData/";
libname output "/OEUS/users/FEDSusers/QCEW/revision_2024/Output/";
 
%include "/OEUS/users/FEDSusers/QCEW/Programs/growthbins_smaller.sas";
%macro analyzequart(YrBeg=,YrEnd=);
proc delete data=output.GrowthDist_20240416;
proc delete data=output.GrowthDist_11_20240416;
proc delete data=output.GrowthDist_22_20240416;

run;

%let counter = 0;

%do z=&YrBeg %to &YrEnd;

	%do j=1 %to 4;
	    
		%put &counter;
                %let qrtyr=&z&j;

                %if &j=1 and &z=1990 %then %do; %end;
                %else %if &j>3 and &z=2015 %then %do; %end;

                %else  %do;
		        %let counter = %eval(&counter + 1);
                        %if &j=1 %then %do;

                                %let zminus=%eval(&z-1);
                                %let qrt=4;
                                %let qrtyrlst=&zminus&qrt;
                                %put &qrtyrlst;
                                        %end;
                        %else %if &j>1 %then %do;
                                %let qrtlst=%eval(&j-1);
                                %let qrtyrlst=&z&qrtlst;
				 %end;
                
		*x gzip -d "/OEUS/users/FEDSusers/QCEW/Data/Intermed/ldb&qrtyr..sas7bdat.gz";
		*x gzip -d "/OEUS/users/FEDSusers/QCEW/Data/Intermed/ldb&qrtyrlst..sas7bdat.gz";
		
		proc sort data=input.ldb&qrtyrlst; by ldb_num;
                proc sort data=input.ldb&qrtyr; by ldb_num;              
		
		data deltan&qrtyr;
		   merge input.ldb&qrtyrlst 
			(keep=ldb_num pred_break_consol_flag m3_empl  
			rename=( m3_empl=m3_empllst) )		   
			 input.ldb&qrtyr
			 (keep=ldb_num naics succ_break_consol_flag own_code m3_empl);
		   by ldb_num;
		   
		   if m3_empl=0 and m3_empllst=0 then delete;
		   deltan=m3_empl-m3_empllst;
		                
                   if m3_empl>0 and m3_empllst>0 then do;
                      continuing=1;
                   end;

     
                                
                  if m3_empl>0 or m3_empllst>0 then do;
				if deltan>0 then do;
					expand=1;
					ngrowth=200*deltan/(m3_empl+m3_empllst);
				end;
 					
 				else if deltan<0 then do;
 					expand=2;
 					ngrowth=-200*deltan/(m3_empl+m3_empllst);
 				end;
 					
                                else if deltan=0 then do;
                                	expand=3;
                                	ngrowth=0;
                                end;
                              
                 end;
                                
                *Note that if continuing=0 and deltan=0 then there was 0 employment is both current and previous quarter.;
 
				%growthbins_smaller;
				bins11 = bins;
				if ngrowth>0 & ngrowth<=1 then bins11=1;
				bins22 = bins;
				if ngrowth>0 & ngrowth<=2 then bins22=1;

                                if own_code~=5 then delete;
                                if succ_break_consol_flag~=0 or pred_break_consol_flag~=0 then delete;


		*Size bin for greater than 20 or not;
		      	      size = 1;
		      	      if m3_empl>20 then size=2;

                                qrt=%eval(&j);
                                yr=%eval(&z);
		
                proc means data=deltan&qrtyr noprint nway;
                class continuing expand size bins;
                output out=means mean(ngrowth)=meanngrowth;

		data means (rename=(_freq_=count)); set means;
                qrt=%eval(&j);
                yr=%eval(&z);

		proc means data=deltan&qrtyr noprint nway;
		class continuing expand size bins11;
		output out=means11 mean(ngrowth)=meanngrowth;

		data means11 (rename=(_freq_=count)); set means11;
		qrt=%eval(&j);
		yr =%eval(&z);
		                
		proc means data=deltan&qrtyr noprint nway;
		class continuing expand size bins22;
		output out=means22 mean(ngrowth)=meanngrowth;

		data means22 (rename=(_freq_=count)); set means22;
		qrt=%eval(&j);
		yr =%eval(&z);		
		

		%if &counter = 1 %then %do; 
		data output.GrowthDist_20240416; set means; run;
		data output.GrowthDist_11_20240416; set means11; run;
		data output.GrowthDist_22_20240416; set means22; run; 
		%end;
		

		%else %if &counter>1 %then %do;
                      proc append base=output.GrowthDist_20240416 data=means;
 		      proc append base=output.GrowthDist_11_20240416 data=means11;
		      proc append base=output.GrowthDist_22_20240416 data=means22;
                %end;
                
		*x gzip "/OEUS/users/FEDSusers/QCEW/Data/Intermed/ldb&qrtyr..sas7bdat.gz";
		*x gzip "/OEUS/users/FEDSusers/QCEW/Data/Intermed/ldb&qrtyrlst..sas7bdat.gz";
		run;
		                                

%end;
%end;
%end;
%mend;


%analyzequart(YrBeg=1990,YrEnd=2015);


*Generate Aggregate Growth Distribution;
proc means data=output.GrowthDist_20240416 noprint nway;
class expand bins;
output out=output.GrowthDist_agg sum(count)=;
run;

*Suppress if any bin has less than 20 obs;
data output.GrowthDist_agg;
set output.GrowthDist_agg;
if count<20 then count=.;
run;

proc export data=output.GrowthDist_agg (keep= expand bins count)
outfile="/OEUS/users/FEDSusers/QCEW/revision_2024/Output/GrowthDist_agg.csv"
dbms=csv replace;
run;

*Generate Aggregate Growth Distribution For Over 20;
proc means data=output.GrowthDist_20240416 noprint nway;
class expand bins;
where size=2;
output out=output.GrowthDist_agg_over20 sum(count)=;
run;
*Suppress if any bin has less than 20 obs;
data output.GrowthDist_agg_over20;
set output.GrowthDist_agg_over20;
if count<20 then count=.;;
run;
proc export data=output.GrowthDist_agg_over20 (keep= expand bins count)
outfile="/OEUS/users/FEDSusers/QCEW/revision_2024/Output/GrowthDist_agg_over20.csv"
dbms=csv replace;
run;

*Generate Aggregate Growth Distribution;
proc means data=output.GrowthDist_11_20240416 noprint nway;
class expand bins11;
output out=output.GrowthDist_11_agg sum(count)=;
run;
*Suppress if any bin has less than 20 obs;
data output.GrowthDist_11_agg;
set output.GrowthDist_11_agg;
if count<20 then count=.;
run;
proc export data=output.GrowthDist_11_agg (keep= expand bins11 count)
outfile="/OEUS/users/FEDSusers/QCEW/revision_2024/Output/GrowthDist_11_agg.csv"
dbms=csv replace;
run;

*Generate Aggregate Growth Distribution;
proc means data=output.GrowthDist_22_20240416 noprint nway;
class expand bins22;
output out=output.GrowthDist_22_agg sum(count)=;
*Suppress if any bin has less than 20 obs;
data output.GrowthDist_22_agg;
set output.GrowthDist_22_agg;
if count<20 then count=.;
run;
proc export data=output.GrowthDist_22_agg (keep= expand bins22 count)
outfile="/OEUS/users/FEDSusers/QCEW/revision_2024/Output/GrowthDist_22_agg.csv"
dbms=csv replace;
run;

run;